home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1990-1992 by Michael Davidson.
- * All rights reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for any purpose and without fee is hereby
- * granted, provided that the above copyright notice appear in all
- * copies and that both that copyright notice and this permission
- * notice appear in supporting documentation.
- *
- * This software is provided "as is" without express or implied warranty.
- */
-
- /*
- * file list handling
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include "vg.h"
-
- STATIC list_t *add_file(char *, list_t *);
- STATIC list_t *add_dir(char *, list_t *);
-
- list_t *
- fileListCreate(
- char **v
- )
- {
- list_t *f;
- char *name;
- struct stat s;
- extern char *cmdname;
-
- f = NULL;
-
- while ((name = *v++) != NULL)
- {
- if (stat(name, &s) == 0)
- {
- switch (s.st_mode & S_IFMT)
- {
- case S_IFREG:
- f = add_file(name, f);
- break;
- case S_IFDIR:
- f = add_dir(name, f);
- break;
- default:
- break;
- }
- }
- else
- {
- fprintf(stderr, "%s: warning - can't stat %s", cmdname, name);
- }
- }
-
- if (f != NULL)
- while (f->prev)
- f = f->prev;
-
- return f;
- }
-
- STATIC list_t *
- add_file(
- char *name,
- list_t *p
- )
- {
- list_t *f;
- char *n;
-
- if ( (f = (list_t *) malloc(sizeof(list_t))) == NULL)
- return p;
-
- if ((n = strdup(name)) == NULL)
- return p;
-
- if (p)
- p->next = f;
- f->prev = p;
- f->next = NULL;
- f->name = n;
- f->flags = 0;
-
- return f;
- }
-
- /*ARGSUSED*/
- STATIC list_t *
- add_dir(
- char *name,
- list_t *p
- )
- {
- return p;
- }
-
- /*
- * fileListSort() - simple insertion sort for file lists
- */
- list_t *
- fileListSort(
- list_t *f
- )
- {
- list_t *p;
- list_t first;
-
- /*
- * set up sentinel element at start of list
- */
- first.prev = NULL;
- first.next = f;
- first.name = "";
- f->prev = &first;
-
- /*
- * sort
- */
- for (f = f->next; f != NULL; f = f->next)
- {
- p = f->prev;
- while (strcmp(f->name, p->name) < 0)
- p = p->prev;
-
- if (p != f->prev)
- {
- /*
- * remove f from current place in list
- */
- if (f->next)
- f->next->prev = f->prev;
- f->prev->next = f->next;
-
- /*
- * insert f into list following p
- */
- f->next = p->next;
- f->next->prev = f;
- p->next = f;
- f->prev = p;
- }
- }
-
- /*
- * remove duplicates
- */
- for (f = first.next; f != NULL; f = f->next)
- {
- while (f->next != NULL && strcmp(f->name, f->next->name) == 0)
- {
- p = f->next;
- f->next = p->next;
- if (f->next)
- f->next->prev = f;
- free(p->name);
- free(p);
- }
- }
-
- /*
- * return pointer to list element following the sentinel
- */
- f = first.next;
- f->prev = NULL;
-
- return f;
- }
-
-